package com.jiuxian.scheduling.vo;

import org.apache.commons.collections.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *
 * @author wangjingqing
 * @date 2017/11/9
 */
public class SchedingVO implements Serializable {

    private static final long serialVersionUID = 1691792991838903403L;
    /** 任务名称（不同任务名称不同）*/
    String name;
    /** 注册路径*/
    String url;
    /** 调用路径 */
    String path;
    /** 执行的方法 */
    String method;
    /** 版本号 */
    String version;
    /** 分组 */
    String group;
    /** 调度的执行时间 */
    String cron;
    /** 目前支持 dubbo、http、local*/
    String protocol;
    /** 参数JSON格式 */
    String parameters;
    /** 执行的服务信息 */
    List<MachineVO> machines = new ArrayList<>();
    /** 开始时间*/
    Date startTime;
    /** 下次执行时间 */
    Date nextDate;
    /** 信息是否有变动 */
    boolean isChange = false;
    /** 当前状态 0：运行中 1：挂起 2：恢复 3：删除 */
    int state = 0;

    public SchedingVO(String name,String url,String path,String method,String group,String version,String cron,String protocol){
        this.name = name;
        this.url = url;
        this.path = path;
        this.method = method;
        this.group = group;
        this.version = version;
        this.cron = cron;
        this.protocol = protocol;
    }
    public SchedingVO(String name,String url,String path,String method,String cron){
        this.name = name;
        this.url = url;
        this.path = path;
        this.method = method;
        this.cron = cron;
        this.protocol = "dubbo";
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getCron() {
        return cron;
    }

    public void setCron(String cron) {
        this.cron = cron;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getParameters() {
        return parameters;
    }

    public void setParameters(String parameters) {
        this.parameters = parameters;
    }

    public List<MachineVO> getMachines() {
        return machines;
    }

    public void setMachines(List<MachineVO> machines) {
        this.machines = machines;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public boolean isChange() {
        return isChange;
    }

    public void setChange(boolean change) {
        isChange = change;
    }

    public void addMachine(MachineVO machine){
        this.machines.add(machine);
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getNextDate() {
        return nextDate;
    }

    public void setNextDate(Date nextDate) {
        this.nextDate = nextDate;
    }

    public void addMachine(String ip, int state){
        MachineVO machine = new MachineVO();
        machine.setIp(ip);
        machine.setState(state);
        machine.setCount(0);
        this.machines.add(machine);
    }
    /**
     * 根据IP获取MachineVO<br>
     * @param ip
     * @return MachineVO
     */
    public MachineVO getMachineVO(String ip){
        if(CollectionUtils.isNotEmpty(this.machines)){
            this.machines.forEach(info->{
                if(ip.equals(info.getIp())){

                }
            });
        }
        return null;
    }

    @Override
    public String toString() {
        return "SchedingVO{" +
                "name='" + name + '\'' +
                ", url='" + url + '\'' +
                ", path='" + path + '\'' +
                ", method='" + method + '\'' +
                ", version='" + version + '\'' +
                ", group='" + group + '\'' +
                ", cron='" + cron + '\'' +
                ", protocol='" + protocol + '\'' +
                ", parameters=" + parameters+
                ", machines=" + machines +
                ", isChange=" + isChange +
                '}';
    }
}
